const express = require('express')
const app = express()
const Controller = require('./util/Controller'),
  controller = new Controller()
const Download = require('./util/Download'),
  download = new Download()

var history = require('connect-history-api-fallback')
app.use('/', history(),express.static(`${__dirname}/client`))

require('./db/index')()
app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Headers', '*')
  res.header('Access-Control-Allow-Methods', 'GET,POST,PUT')
  if (req.method === 'OPTIONS') {
    res.status = 200;
    res.send();
    return;
  }
  next()
})
// 文件校验
app.post('/verify', async (req, res) => {
  await controller.handleVerifyUpload(req, res)
})
// 上传文件
app.post('/upload',async (req, res) => {
  await controller.handleFormData(req, res)
})
// 文件合并
app.post('/merge',async  (req, res) => {
  await controller.handleMerge(req, res)
})

// 文件查询
app.get('/getfiles', async (req, res) => {
  await download.getfiles(req, res)
})
// 添加文件
app.post('/addfile', async (req, res) => {
  await download.addfile(req, res)
})
// 删除文件
app.put('/deletefile', async (req, res) => {
  await download.deletefile(req, res)
})
app.get('/downloadfile', async (req, res) => {
  await download.downloadfile(req, res)
})
app.listen(3001, _=>(console.log('http://localhost:3001')))